package com.lepu.app.fun.grow.setting;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.app.application.MyApplication;
import com.app.config.UserConfig;
import com.app.utils.Const;
import com.app.utils.PlaySoundUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.eyzhs.app.R;
import com.lepu.app.fun.grow.adapter.GrowSettingMusicAdapter;
import com.lepu.app.main.activity.MainTabActivity;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

public class GrowSettingMusicActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener,AsyncRequest {

	@Override
	@SuppressLint("InflateParams")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grow_setting_music_activity);

		init();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		
		PlaySoundUtil.getInstance().stop(null);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("设置背景音乐");
		topbar.setonTopbarNewLeftLayoutListener(this);

		int selected = UserConfig.getConfigInt(this, Const.SETTING_MUSIC_INDEX_SELECTED, 0);
		final String[] titleArray = getResources().getStringArray(
				R.array.array_play_sample_music);

		final ListView dataListView = (ListView) findViewById(R.id.dataListView);
		dataListView.setAdapter(new GrowSettingMusicAdapter(this, dataListView,titleArray,selected));

		dataListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				boolean flag = MainTabActivity.jumpLogin(GrowSettingMusicActivity.this);
				if(flag)
				{
					return;
				}
				
				for(int i=0;i<titleArray.length;i++)
				{
					String rightTag = String.format("right_%d", i);
					ImageView rightImageView = (ImageView) dataListView.findViewWithTag(rightTag);
					if(rightImageView != null)
					{
						rightImageView.setSelected(false);
					}
				}
				
				UserConfig.setConfig(getApplicationContext(),
						Const.SETTING_MUSIC_INDEX_SELECTED, position);
				String rightTag = String.format("right_%d", position);
				ImageView rightImageView = (ImageView) dataListView.findViewWithTag(rightTag);
				if(rightImageView != null)
				{
					rightImageView.setSelected(true);
				}

				saveMusicIndex();
			}
		});
	}
	
	private void saveMusicIndex()
	{
		if(MyApplication.getInstance().checkIsLogin())
		{
			int selectedIndex = UserConfig.getConfigInt(this, Const.SETTING_MUSIC_INDEX_SELECTED, 0);
			LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
			String url = Setting.getModiyAlbumMusicUrl();

			Map<String, Object> map = new HashMap<String, Object>();
			JSONObject params = new JSONObject();
			try {
				params.put("DeviceID", Setting.DEVICEID);
				params.put("ApplicationID", Setting.APPLICATIONID);
				params.put("UserID",loginInfo.UserID);
				params.put("LoginToken",loginInfo.LoginToken);

				params.put("AlbumMusicID", String.valueOf(selectedIndex));
			} catch (Exception e) {
			}
			map.put("ht", params);
			
			showProgressDialog();
			ApiClient.http_post_main(url, map, null, this, "", false);
		}
	}

	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void RequestComplete(Object object, Object data) {
		hideProgressDialog();
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		hideProgressDialog();
	}
}
